package com.shop.cloud.service.hongfu.impl;

import com.google.gson.Gson;
import com.shop.cloud.domain.hongfu.GoodsCategory;
import com.shop.cloud.service.hongfu.IGoodsCategoryService;
import com.shop.cloud.util.Page;
import com.shop.cloud.dao.hongfu.GoodsCategoryDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;

/**
 * goods_category
 */
@Service("goodsCategoryService")
public class GoodsCategoryService extends BaseService<GoodsCategory> implements IGoodsCategoryService{
    
    @Autowired
	private GoodsCategoryDao goodsCategoryDao;
    
    @Autowired
    private GoodsCategoryService goodsCategoryService;

	@Override
	public MybatisDao<GoodsCategory> getEntityDao() {
		return goodsCategoryDao;
	}
	
	public List<GoodsCategory> find2(GoodsCategory goodsCategory) {
		return goodsCategoryDao.findByEntity2(goodsCategory);
	}
	
	public Page<GoodsCategory> catePage(GoodsCategory entity) {
		entity.setPageSize((entity.getPageSize_()==null?20:entity.getPageSize_()));
//		long count = goodsCategoryDao.countByEntity2(entity);
		Page<GoodsCategory> page = new Page<GoodsCategory>(entity.getPageIndex_(), entity.getPageSize_());
		List<GoodsCategory> list=goodsCategoryDao.findByEntity2(entity);
		List<GoodsCategory> list3=goodsCategoryDao.findByEntity3(entity);
		List<GoodsCategory> list2=new ArrayList<GoodsCategory>();
		for (int i = 0; i < list.size(); i++) {
			GoodsCategory goodsCategory =list.get(i);
			
			if (goodsCategory.getParentId()!=0) {
				GoodsCategory g2 =new GoodsCategory();
				g2.setId(new Long((long)goodsCategory.getParentId()));
				GoodsCategory goodsCategory1 = goodsCategoryDao.get(new Long((long)goodsCategory.getParentId()));
				
				goodsCategory.setId(list.get(i).getId());
				if (goodsCategory1!=null) {
					goodsCategory.setFirstName(goodsCategory1.getName());
				}
				goodsCategory.setSecondName(goodsCategory.getName());
				goodsCategory.setTypeName(goodsCategory.getType()+"");
				goodsCategory.setpId(goodsCategory.getParentId());
				list2.add(goodsCategory);
			}
			
			if(goodsCategory.getParentId()==0) {
				GoodsCategory	g3 = new GoodsCategory();
				g3.setParentId(goodsCategory.getId().intValue());
				GoodsCategory goodsCategory1 = goodsCategoryService.findOne(g3);
					goodsCategory.setpId(goodsCategory.getId());
					goodsCategory.setFirstName(goodsCategory.getName());
					goodsCategory.setTypeName(goodsCategory.getType()+"");
					list2.add(goodsCategory);
					
			}
			
		}
		
	
		page.setData(list2);
		page.setTotalCount(list3.size());
		return page;
	}
    
}